package com.sgb.goods.viewmodel;

import androidx.databinding.ObservableField;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.utils.StringUtils;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 添加/编辑品牌
 * @author cyj
 */
public class AddBrandViewModel extends BaseViewModel {

    public String id = "";
    /** 品牌名称 */
    public ObservableField<String> brandName = new ObservableField<>("");
    /** 备注描述 */
    public ObservableField<String> remarkDesc = new ObservableField<>("");

    /**
     * 保存品牌
     */
    public void saveBrand() {
        // 品牌名称 校验
        if(StringUtils.isEmpty(brandName.get())) {
            MToast.showToast(application.getApplicationContext(), "请输入品牌名称");
            return;
        }

        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID, "");
        Map<String, String> map = new HashMap<>(4);
        map.put("id", StringUtils.isEmpty(id) ? "" : id);
        map.put("brandName", Objects.requireNonNull(brandName.get()));
        map.put("mark", Objects.requireNonNull(remarkDesc.get()));
        map.put("companyId", companyId);

        showLoading();
        GoodsNet.getInstance().getGoodsApi().saveBrand(map).enqueue(new GoodsCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                finishActivity(999);
            }
        });
    }
}
